//
//  QianliNotforgetVC.swift
//  todoios
//
//  Created by qianli on 2025/4/23.
//

import Foundation
import SwiftyUserDefaults

class QianliNotforgetVC: QianliParentNaviVC {
    private var ELYefP6SwtfJIUTF:UIImageView?
    private var efBNTP1TWW1haLFq:UIImage?
    deinit {
        QianliUniversalFunc.QianliLog("------QianliNotforgetVC.deinit")
    }
    
    override func GHL4Rhb91XYBer27() {
        zdMpsGrNncVEHdYA(title: LocalizedString("ANTI-LOSS"))
        
        let backView = UIImageView(frame: CGRect(x: qianli_pm_width/2 - 100, y: topSpace+50, width: 200, height: 200))
        view.addSubview(backView)
        backView.backgroundColor = .white
        
        self.ELYefP6SwtfJIUTF = UIImageView(frame: CGRect(x: 10, y: 10, width: 180, height: 180))
        if let qrcodeView = self.ELYefP6SwtfJIUTF{
            backView.addSubview(qrcodeView)
            qrcodeView.qianli_image(imgurl: "qianliLoading.gif")
            qrcodeView.backgroundColor = .white
        }
        
        let titleLabel = UILabel(frame: CGRect(x: 15, y: backView.frame.maxY, width: qianli_pm_width-30, height: 60))
        view.addSubview(titleLabel)
        titleLabel.textAlignment = .center
        titleLabel.font = .systemFont(ofSize: 16)
        titleLabel.textColor = .label
        titleLabel.text = LocalizedString("Save the QR code to stay connected forever!")//"保存二维码，永不失联!"
        
        let subLabel = UILabel(frame: CGRect(x: 50, y: titleLabel.frame.maxY, width: qianli_pm_width-100, height: 60))
        view.addSubview(subLabel)
        subLabel.numberOfLines = 0
        subLabel.font = .systemFont(ofSize: 14)
        subLabel.textColor = .secondaryLabel
        subLabel.text = LocalizedString("Break free from restrictions and enjoy internet freedom forever! We recommend saving the QrCode to your album as a backup.")
        
        
        let btn = GradientBtn.init(frame: CGRect(x: qianli_pm_width/2 - 120/2, y: subLabel.frame.maxY+30, width: 120, height: 42), title: LocalizedString("Save to Album"))
        view.addSubview(btn)
        btn.titleLabel?.font = .systemFont(ofSize: 15)
        btn.touch_With_Block { [weak self] sender in
            if let self = self{
                if let image = self.efBNTP1TWW1haLFq{
                    self.y7YWp6MipTcFhFXU(image)
                }else{
                    self.view.Qianli_Toast(LocalizedString("Valid QR code not generated yet. Please wait..."))
                }
            }
        }
    }
    
    override func aB7kFqI8MB6DRPQC() {
        DispatchQueue.global().async {
            let frontendURL = Defaults[\.frontendUrl]
            let imgUrl = "\(frontendURL)/#/login?aff=" + Defaults[\.invitationCode]
            if let qrCodeImage = QianliQRCode.generate_qianli_qrcode_withLogo(from: imgUrl, logo: "qianli_logo_main".qianli_R_image){
                DispatchQueue.main.async {[weak self] in
                    if let self = self{
                        self.efBNTP1TWW1haLFq = qrCodeImage
                        if let qrcodeView = self.ELYefP6SwtfJIUTF{
                            qrcodeView.image = qrCodeImage
                        }
                    }
                }
            }
            
        }
    }
}

//MARK: - 保存到相册
extension QianliNotforgetVC{
//保存image到相册
    private func y7YWp6MipTcFhFXU(_ image:UIImage?) {
        if image != nil{
            if let upImage = image?.qianli_ajust_albumphoto_orientation(){
                UIImageWriteToSavedPhotosAlbum(upImage,self,#selector(f6a0uSc6hoEZjL4z),nil)
            }
        }
    }
    
    @objc func f6a0uSc6hoEZjL4z(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if error != nil{
            self.view.Qianli_Toast(LocalizedString("Save Failed"))
        }else{
            self.view.Qianli_Toast(LocalizedString("Save Successful"))
        }
    }
}
